package com.whuan.student.service.impl;

import com.whuan.student.convert.Object2Student;
import com.whuan.student.dataObject.PageObject;
import com.whuan.student.dataObject.Student;
import com.whuan.student.dto.StudentDTO;
import com.whuan.student.enums.ResultEnum;
import com.whuan.student.exceptions.StudentException;
import com.whuan.student.repository.StudentRepository;
import com.whuan.student.repository.StudentRepositoryClass;
import com.whuan.student.service.StudentService;
import com.whuan.student.util.IDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    private StudentRepositoryClass studentRepositoryClass = new StudentRepositoryClass();
    @Override
    public Student findById(String studentId) {
        if(studentId.length()==0){
            throw new StudentException(ResultEnum.PARAM_ERROR);
        }
        Optional<Student> StudentOptional = studentRepository.findById(studentId);
        Student student = StudentOptional.get();
        return student;
    }

    @Override
    public List<Student> findAll() {
        List<Student> studentList = studentRepository.findAll();
        return studentList;
    }

    @Override
    public List<Student> findByUpClass() {
        List<Student> studentList = studentRepository.findByUpclass(1);
        return studentList;
    }

    /**
     * create by: whaun
     * create date:  2019/8/27 time:7:03
     * description:studentDTO 包含 姓名 密码 job sgroup sclass
     */
    @Override
    public Student save(StudentDTO studentDTO) {
        // 检查用户名
        if(studentDTO==null || studentDTO.getStudentName() == null||studentDTO.getStudentName().length()==0){
            throw new StudentException(ResultEnum.PARAM_ERROR);
        }
        //检查是添加还是更改
        if(studentDTO.getStudentId()==null || studentDTO.getStudentId().length()==0){
            studentDTO.setStudentId(getStudentId());
        }
        //密码如果为空，设置默认值“111111”
        if(studentDTO.getStudentPassword()==null||studentDTO.getStudentPassword().length()==0){
            studentDTO.setStudentPassword("111111");
        }
        //班级成员标志如果为空，设置默认值“1”
        if(studentDTO.getUpclass()==null||studentDTO.getStudentPassword().length()==0){
            studentDTO.setUpclass(1);
        }
        Student student = new Student();

        BeanUtils.copyProperties(studentDTO,student);
        Student save = studentRepository.save(student);
        return save;
    }

    @Override
    @Transactional
    public Integer save(List<StudentDTO> studentDTOList) {
        for(int i = 0;i< studentDTOList.size();i++){
            studentDTOList.get(i).setStudentId(IDUtils.getBatchStudentId(i));
        };
        List<Student> studentList = studentDTOList.stream()
                .map(e  -> {
                    Student student = new Student();
                    BeanUtils.copyProperties(e, student);
                    student.setUpclass(1);
                    return student;
                })
                .collect(Collectors.toList());
        log.info("studentList = {}",studentList);
        log.info("studentList.size = {}",studentList.size());
        List<Student> students = studentRepository.saveAll(studentList);
/*        Integer i = studentRepositoryClass.batchInsert(studentList);
        log.info("studentListLength = {}",i);*/
        return students.size();
    }

    @Override
    public Page<Student> findByUpClassWithPage(PageObject pageObject) {
        Pageable pageable = PageRequest.of(pageObject.getPage()-1, pageObject.getLimit(),Sort.Direction.ASC, "studentId");
        //return studentRepository.findAll(pageable);
        Integer upclass = 1;
        return studentRepository.findByUpclass(upclass,pageable);
    }

    @Override
    public List<Student> findByName(String name) {
        List<Object> objectList = studentRepository.findByName("陈文静");
        List<Student> students = new ArrayList<>();
        for(Object o :objectList){
            Student student = Object2Student.getByObject(o);
            students.add(student);
        }
        return students;
    }

    public String getStudentId(){
        String studentId = IDUtils.getStudentId();
        Optional<Student> optionalStudent = studentRepository.findById(studentId);
        if(studentRepository.findById(studentId).isPresent()){
            studentId = getStudentId();
        }
        return studentId;
    }
}
